//
//  AddressViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by Bazinga on 2018/8/10.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "AddressViewController.h"
#import "AddNewConsigneeAddressViewController.h"
#import "LocationTableViewCell.h"

#import "AddressListModel.h"

static NSString *kLocationTableViewCell = @"LocationTableViewCell.h";

@interface AddressViewController () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) NSArray *modelsArr;


@end

@implementation AddressViewController

/**
 生命周期
 */
#pragma mark - LifeCyle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收货信息";
    
    [self setupUI];
    [self setupLayout];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
}

- (void)loadData {
    
    NSDictionary *param = @{@"siteId":@"32"};
    [MLAHttpManage postHttpType:MLAHttpRequestType_Post hostType:MLAHttpHostType_Api api:@"/api/plugins/ss.shopping/ApiPayGet" params:param.mj_JSONString success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        _modelsArr = [AddressListModel mj_objectArrayWithKeyValuesArray:response[@"addressInfoList"]];
        [_tableView reloadData];
    } failure:^{
        
    }];

}

/**
 创建
 */
#pragma mark - Intial Methods
#pragma mark- UI
- (void)setupUI{
    self.view.backgroundColor = UIColorFromRGBHex(0xf2f2f4);
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.backgroundColor = self.view.backgroundColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 100;
    self.tableView.estimatedSectionHeaderHeight = 10;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[LocationTableViewCell class] forCellReuseIdentifier:kLocationTableViewCell];

    [self.view addSubview:self.tableView];
    

    
    self.nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextButton.frame = CGRectMake(20, 50, SCREEN_WIDTH-40, 40);
    self.nextButton.backgroundColor = UIColorFromRGBHex(0xfa7a09);
    self.nextButton.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightRegular];
    self.nextButton.layer.cornerRadius = 5.0f;
    self.nextButton.layer.masksToBounds = YES;
    
    [self.nextButton setTitle:@"添加新地址" forState:UIControlStateNormal];
    [self.nextButton setTitle:@"添加新地址" forState:UIControlStateHighlighted];
    WEAK_SELF
    [[self.nextButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        STRONG_SELF
        AddNewConsigneeAddressViewController *add = [[AddNewConsigneeAddressViewController alloc] init];
        [self.navigationController pushViewController:add animated:YES];
    }];
    [self.view addSubview:self.nextButton];
    [_nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.right.offset(-20);
        make.bottom.offset(-30);
        make.height.mas_equalTo(40);
    }];

    
}
- (void)setupLayout {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.bottom.offset(-70);
    }];
}

/**
 tableView代理、数据源
 */
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _modelsArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LocationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kLocationTableViewCell forIndexPath:indexPath];
    cell.curModel = _modelsArr[indexPath.section];
    WEAK_SELF
    cell.clickBlock = ^(NSInteger index, AddressListModel *model) {
        STRONG_SELF
        [self clickType:index withModel:model];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}


#pragma mark - 点击事件
- (void)clickType:(NSInteger)index withModel:(AddressListModel *)model {
    //1.设为默认地址 2.编辑 3.删除

    bool bool_true = true;
    bool bool_false = false;
    
    if (index == 1) {
        model.isDefault = @(bool_true);
        model.isEditOrNew = @(bool_false);
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:model.mj_JSONObject];
        [params removeObjectForKey:@"userName"];

        NSDictionary *param = @{@"delivery": @"5",
                                @"isEdit": @(bool_true),
                                @"siteId": @"32",
                                @"addressInfo":params
                                };
        
        [MLAHttpManage postHttpType:MLAHttpRequestType_Post hostType:MLAHttpHostType_Api api:@"/api/plugins/ss.shopping/ApiPaySaveAddress" params:param.mj_JSONString success:^(id  _Nonnull response, NSString * _Nonnull msg) {
            [self loadData];
        } failure:^{
            
        }];
    } else if (index == 2) {
        AddNewConsigneeAddressViewController *editVC = [[AddNewConsigneeAddressViewController alloc] init];
        editVC.model = model;
        [self.navigationController pushViewController:editVC animated:YES];
        
    } else if (index == 3) {
        
        NSDictionary *param = @{@"addressId":model.id};
        
        [MLAHttpManage postHttpType:MLAHttpRequestType_Post hostType:MLAHttpHostType_Api api:@"/api/plugins/ss.shopping/ApiPayRemoveAddress" params:param.mj_JSONString success:^(id  _Nonnull response, NSString * _Nonnull msg) {
            [self loadData];

        } failure:^{
            
        }];
    }
}


@end
